package com.edu.hre.db.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;



import com.edu.hre.db.config.BaseDAO;
import com.edu.hre.db.config.DBPool;
import com.edu.hre.db.dto.HreEmpBasicsalary;
import com.edu.hre.db.dto.HreEmpEmergencyContact;
import com.edu.hre.db.dto.HreEmpDependent;


public class EmpSalaryDAO  extends BaseDAO {
	private static final Logger logger = Logger.getLogger(EmpSalaryDAO.class);
	public List<HreEmpBasicsalary> getSalaryByEmployeeId(
			int employeeNumber) {
		List<HreEmpBasicsalary> listSalaries = new ArrayList<HreEmpBasicsalary>();
		Connection conn = null;
		PreparedStatement preStmt = null;
		ResultSet rs = null;
		try {
			conn = DBPool.getConnection();
			String strSQLQuery = "SELECT bs.salary_component " +
			",bs.ebsal_basic_salary " +
			",ct.currency_id " +
			",ct.currency_name " +
			",ppt.payperiod_name " +
			"FROM hre_emp_basicsalary bs " +
			"JOIN hre_pay_grade pg ON bs.sal_grd_code = pg.id " +
			"JOIN hre_currency_type ct ON pg.currency_id = ct.currency_id "+
			"JOIN hre_pay_period_type ppt ON bs.payperiod_code = ppt.payperiod_code "+
			"WHERE bs.emp_number = "
					+ employeeNumber;
			preStmt = conn.prepareStatement(strSQLQuery);
			rs = preStmt.executeQuery();
			while (rs.next()) {
				HreEmpBasicsalary empSalary = new HreEmpBasicsalary();
				empSalary.setSalaryComponent(rs.getString("salary_component"));
				empSalary.setEbsalBasicSalary(rs.getDouble("ebsal_basic_salary"));
				empSalary.setCurrencyId(rs.getString("currency_id"));
				empSalary.setCurrencyName(rs.getString("currency_name"));
				empSalary.setPayPeriodName(rs.getString("payperiod_name"));
				//.....
				//.....
				listSalaries.add(empSalary);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally {
			// đóng PreparedStatement, ResultSet và đẩy connection vào pool
			releaseConnection(conn, preStmt, rs);
		}
		return listSalaries;
	}

}
